//
//  GetCollectionsOperation.m
//  VMIX
//
//  Created by jeffs on 5/21/10.
//  Copyright 2010 Vmix Media, INC. All rights reserved.
//

#import "GetCollectionsOperation.h"
#import "VmixCollection.h"

@implementation GetCollectionsOperation
@synthesize collectionsData, collectionsArray, delegate, collectionsApi;


- (void)main {
	
	NSURL *url = [NSURL URLWithString: self.collectionsApi];
	
	CXMLDocument *apiParser = [[[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil] autorelease];
    self.collectionsArray = [[NSMutableArray alloc] init];
	NSArray *resultNodes = NULL;
    resultNodes = [apiParser nodesForXPath:@"//collection" error:nil];
    for (CXMLNode *resultNode in resultNodes) {
		
		VmixCollection *collection = [[VmixCollection alloc] init];
		collection.collection_id = [resultNode getElementValue:@"id" error:nil];
		collection.token = [resultNode getElementValue:@"token" error:nil];
		collection.name = [resultNode getElementValue:@"name" error:nil];
		collection.description = [resultNode getElementValue:@"description" error:nil];
		collection.status = [resultNode getElementValue:@"status" error:nil];
		collection.type = [resultNode getElementValue:@"type" error:nil];
		collection.date_created = [resultNode getElementValue:@"date_created" error:nil];
		collection.last_modified =[ resultNode getElementValue:@"last_modified" error:nil];
		collection.search_query = [resultNode getElementValue:@"search_query" error:nil];
		collection.owner_partner_id = [resultNode getElementValue:@"owner_partner_id" error:nil];
		collection.thumbnail_media_id = [resultNode getElementValue:@"thumbnail_media_id" error:nil];
		
		if([resultNode getElementValue:@"parent" error:nil] != nil ){
			
			[self.collectionsArray addObject:collection];
		}

    }	
	
	[self.delegate didFinishParsingCollections:self.collectionsArray];
	
}


@end

